import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layouts/RootPage.vue'
import BlankPage from '@/layouts/BlankPage.vue'
import { getCache } from '@/utils/cache'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Layout,
      redirect: '/index',
      children: [
        {
          path: 'index',
          name: 'home',
          meta: {
            title: '首页'
          },
          component: () => import('@/views/home/HomeView.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('@/views/Login/LoginView.vue')
    },
    {
      path: '/collection',
      name: 'collection',
      component: BlankPage,
      redirect: '/collection/index',
      children: [
        {
          path: 'index',
          name: 'collection-index',
          meta: {
            title: '订单记录'
          },
          component: () => import('@/views/collection/order.vue')
        },
        {
          path: 'info',
          name: 'collection-info',
          meta: {
            title: '收款管理'
          },
          component: () => import('@/views/collection/info.vue')
        }
      ]
    },
    {
      path: '/payment',
      name: 'payment',
      component: BlankPage,
      redirect: '/payment/index',
      children: [
        {
          path: 'index',
          name: 'payment-index',
          meta: {
            title: '代付管理'
          },
          component: () => import('@/views/payment/index.vue')
        }
      ]
    },
    {
      path: '/group',
      name: 'group',
      component: BlankPage,
      redirect: '/group/index',
      children: [
        {
          path: 'index',
          name: 'group-index',
          meta: {
            title: '下级开户'
          },
          component: () => import('@/views/group/index.vue')
        }
      ]
    },
    {
      path: '/withdrawal',
      name: 'withdrawal',
      component: BlankPage,
      redirect: '/withdrawal/index',
      children: [
        {
          path: 'index',
          name: 'withdrawal-index',
          meta: {
            title: '提现管理'
          },
          component: () => import('@/views/withdrawal/index.vue')
        }
      ]
    },
    {
      path: '/user',
      name: 'user',
      component: BlankPage,
      redirect: '/user/index',
      children: [
        {
          path: 'index',
          name: 'user-index',
          meta: {
            title: '我的'
          },
          component: () => import('@/views/user/index.vue')
        },
        {
          path: 'msg',
          name: 'user-msg',
          meta: {
            title: '系统消息'
          },
          component: () => import('@/views/user/sysMsg.vue')
        },
        {
          path: 'moeny',
          name: 'user-moeny',
          meta: {
            title: '资金明细'
          },
          component: () => import('@/views/user/moeny.vue')
        },
        {
          path: 'loginLog',
          name: 'user-loginLog',
          meta: {
            title: '登录记录'
          },
          component: () => import('@/views/user/log.vue')
        },
        {
          path: 'order',
          name: 'user-order',
          meta: {
            title: '统计订单'
          },
          component: () => import('@/views/user/order.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to) => {
  document.title = to.meta.title as string
  if (to.name !== 'login' && (getCache('token') == null && getCache('virtualToken') == null)) {
    return { name: 'login' }
  } else if (to.name === 'login' && getCache('token') != null) {
    return { name: 'home' }
  }
})

export function useGoto(name: string, params?: any) {
  router.push({ name, params })
}

export default router
